<template>
    <el-dialog
        title="添加背景图片"
        :visible.sync="visible"
       
        :before-close="close"
        :append-to-body="true"
       >
        <el-form ref="form">
            <el-form-item label="上传图片">
                <input type="file" ref="referenceUpload" accept="jpg,png" @change="fileChange($event)">
            </el-form-item>
            
        </el-form>
        <!-- <span slot="footer" class="dialog-footer">
            <el-button @click="close">取 消</el-button>
            <el-button type="primary" @click="comfirm">确 定</el-button>
        </span> -->
        </el-dialog>
</template>
<script>
import COS from '@/util/COS';
export default {
    props: {
        visible: {
            type: Boolean,
            default: false
        }
    },
    data(){
        return {
            url: ''
        }
    },
    methods: {
        close(){
            this.$emit('close')
        },
        comfirm(){
            this.close()
           

            window.fabricCanvas.addBackground({
                url: this.url
            })
        },

         //上传图片
        fileChange(el){
            
                let _this = this;
                let file = el.target.files[0];
                let size = file.size;
                let name = file.name;
                let fileName = name.substring(name.lastIndexOf(".")+1).toLowerCase();

                if(fileName == 'jpg' || fileName == 'png' || fileName == 'gif' || fileName == 'jpeg' ){

                }else{
                    _this.$message.warning('请上传jpg、png、gif、jpeg格式');
                    this.$refs.referenceUpload.value = ''
                    return false
                }

               
                COS.putObject(file).then(url => {
                   this.$refs.referenceUpload.value = ''
                   this.url = url
                   this.comfirm()
                })
            
        },
       
    }
}
</script>
<style scoped>

</style>